﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FX.CC.Andon.Utility.DataBase;
using FX.CC.Andon.Entity.Service;
using System.Data;
using FX.CC.Andon.Utility.Common;

namespace FX.CC.Andon.Service.Service
{
    public class EventRecService : DbHelperBase
    {
        public IList<EventRecWrapper> GetEventRec(DateTime lastGetTime, out DateTime newGetTime)
        {
            string sql =
@"Select @dbGetTime = getdate();
SELECT WS.WSName, Ln.LineName, EQ.EqCode, EQ.EqName, Rec.ETypeCode, ET.ETypeName, Detail.DetailName, 
      Usr.FullName, Rec.StartTime
FROM UT_EventRec AS Rec INNER JOIN
      UT_Equipment AS EQ ON Rec.EqID = EQ.EqID INNER JOIN
      UT_EType AS ET ON Rec.ETypeCode = ET.ETypeCode INNER JOIN
      UT_ETypeDetail AS Detail ON Rec.DetailID = Detail.DetailID INNER JOIN
      UT_User AS Usr ON Rec.StartUserID = Usr.UserID INNER JOIN
      UT_Line AS Ln ON EQ.LineID = Ln.LineID INNER JOIN
      UT_WorkShop AS WS ON Ln.WSID = WS.WSID
WHERE (Rec.StartTime > @lastGetTime);";

            List<EventRecWrapper> list = new List<EventRecWrapper>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                Db.AddInParameter(cmd, "@lastGetTime", DbType.DateTime, lastGetTime);
                Db.AddOutParameter(cmd, "@dbGetTime", DbType.DateTime);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var obj = new EventRecWrapper();
                            obj.WSName = ReaderHelper.GetValue<string>(reader, "WSName");
                            obj.LineName = ReaderHelper.GetValue<string>(reader, "LineName");
                            obj.EqCode = ReaderHelper.GetValue<string>(reader, "EqCode");
                            obj.EqName = ReaderHelper.GetValue<string>(reader, "EqName");
                            obj.ETypeCode = ReaderHelper.GetValue<string>(reader, "ETypeCode");
                            obj.ETypeName = ReaderHelper.GetValue<string>(reader, "ETypeName");
                            obj.DetailName = ReaderHelper.GetValue<string>(reader, "DetailName");
                            obj.FullName = ReaderHelper.GetValue<String>(reader, "FullName");
                            obj.StartTime = ReaderHelper.GetValue<DateTime>(reader, "StartTime");
                            list.Add(obj);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                newGetTime = (DateTime)cmd.Parameters["@dbGetTime"].Value;
                return list;
            }
        }

        public IList<string> GetMailList(string eTypeCode) {
            string sql = "Select MailAddr From UT_ETypeMail Where ETypeCode=@code;";

            IList<string> list = new List<string>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                Db.AddInParameter(cmd, "@code", DbType.String, eTypeCode);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            string addr = (string)reader[0];
                            list.Add(addr);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }
        }
    }
}
